package com.baijiayun.liveuibase.toolbox.screenshare

import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.ContextCompat
import com.baijiayun.livecore.utils.LPRxUtils
import com.baijiayun.liveuibase.R
import com.baijiayun.liveuibase.base.BaseDialogFragment
import com.baijiayun.liveuibase.utils.DisplayUtils
import com.baijiayun.liveuibase.utils.RxUtils
import com.baijiayun.liveuibase.utils.ThemeDataUtil
import com.baijiayun.liveuibase.utils.drawable.DrawableBuilder
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.bjy_base_dialog_screen_share.view.*
import java.util.concurrent.TimeUnit

/**
 * Created by pengwenjian on 2021/9/29
 * Describe:
 */
class ScreenShareDialog : BaseDialogFragment() {
    var screenShareListener: ScreenShareListener? = null
    var isAudioOn = true
    private var disposables: CompositeDisposable = CompositeDisposable()
    override fun getLayoutId() = R.layout.bjy_base_dialog_screen_share

    override fun init(savedInstanceState: Bundle?, arguments: Bundle?) {
        // do nothing
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        view.screen_share_audio_iv.setImageDrawable(
            AppCompatResources.getDrawable(
                context!!,
                R.drawable.bjy_base_sel_audio_state
            )
        )
        updateScreenShareAudioState(isAudioOn)
        view.screen_share_stop_iv.setOnClickListener {
            screenShareListener?.onScreenShareStop()
        }
        view.screen_share_home_iv.setOnClickListener {
            screenShareListener?.onScreenShareBackToHome()
        }
        disposables.add(RxUtils.clicks(view.screen_share_audio_iv)
            .throttleFirst(500, TimeUnit.MILLISECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                if (view.screen_share_audio_iv.isSelected) {
                    screenShareListener?.onScreenShareAudioOn()
                } else {
                    screenShareListener?.onScreenShareAudioOff()
                }
            })
    }

    fun updateScreenShareAudioState(isAudioOn:Boolean) {
        view?.let {
            it.screen_share_audio_iv.isSelected = !isAudioOn
            it.screen_share_audio_tv.text = if (!isAudioOn) {
                getString(R.string.bjy_base_share_screen_audio_off)
            } else {
                getString(R.string.bjy_base_share_screen_audio_on)
            }
        }
    }

    override fun onStart() {
        super.onStart()
        hideTitleBar()
        dialog?.setCanceledOnTouchOutside(false)
        dialog?.setCancelable(false)
        dialog?.window?.setBackgroundDrawable(DrawableBuilder()
            .rectangle()
            .solidColor(ContextCompat.getColor(context!!,R.color.base_main_color_layer_10))
            .cornerRadius(resources.getDimensionPixelSize(R.dimen.bjy_base_common_bg_radius))
            .build())
    }

    override fun onDestroyView() {
        super.onDestroyView()
        LPRxUtils.dispose(disposables)
    }

    override fun setWindowParams(windowParams: WindowManager.LayoutParams?) {
        super.setWindowParams(windowParams)
        windowParams?.let {
            it.width = DisplayUtils.dip2px(context!!, 480f)
            it.height = WindowManager.LayoutParams.WRAP_CONTENT
            it.gravity = Gravity.CENTER
            it.dimAmount = 1.0f
        }
    }
    interface ScreenShareListener{
        fun onScreenShareAudioOn()
        fun onScreenShareAudioOff()
        fun onScreenShareStop()
        fun onScreenShareBackToHome()
    }
}